package com.kma.summer2012.model;
// Generated Dec 6, 2012 12:14:41 AM by Hibernate Tools 3.2.1.GA


import com.kma.summer2012.facade.DateAdapter;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@XmlRootElement(name = "request")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "CRUMBS_REQUEST"
        , schema = "KMA1"
)
public class CrumbsRequest implements java.io.Serializable {


    private int id;
    @XmlElement(name = "client")
    private CrumbsClient crumbsClient;
    private CrumbsRequestsState crumbsRequestsState;
    @XmlElement(name = "dateOfCreation")
    @XmlJavaTypeAdapter(DateAdapter.class)
    private Date dateOfCreation;
    @XmlElement(name = "requestInfo")
    private String requestInfo;
    private Integer actualTimeOfWork;
    private Set<CrumbsUser> crumbsUsers = new HashSet<CrumbsUser>(0);  //engineer
    private Set<CrumbsProcessHistory> crumbsProcessHistories = new HashSet<CrumbsProcessHistory>(0);
    private Set<CrumbsAdditionalTools> crumbsAdditionalTools = new HashSet<CrumbsAdditionalTools>(0);
    @XmlElement(name = "works")
    private Set<CrumbsRequestAndWork> crumbsRequestAndWorks = new HashSet<CrumbsRequestAndWork>(0);
    @XmlElement(name = "requestedDateAndTime")
    private Set<CrumbsDateAndTime> crumbsDateAndTimes = new HashSet<CrumbsDateAndTime>(0);

    public CrumbsRequest() {
    }


    public CrumbsRequest(int id, CrumbsClient crumbsClient, CrumbsRequestsState crumbsRequestsState, Date dateOfCreation) {
        this.id = id;
        this.crumbsClient = crumbsClient;
        this.crumbsRequestsState = crumbsRequestsState;
        this.dateOfCreation = dateOfCreation;
    }

    public CrumbsRequest(int id, CrumbsClient crumbsClient, CrumbsRequestsState crumbsRequestsState,
                         Date dateOfCreation, String requestInfo, Integer actualTimeOfWork,
                         Set<CrumbsUser> crumbsUsers, Set<CrumbsProcessHistory> crumbsProcessHistories,
                         Set<CrumbsAdditionalTools> crumbsAdditionalTools,
                         Set<CrumbsRequestAndWork> crumbsRequestAndWorks,
                         Set<CrumbsDateAndTime> crumbsDateAndTimes) {
        this.id = id;
        this.crumbsClient = crumbsClient;
        this.crumbsRequestsState = crumbsRequestsState;
        this.dateOfCreation = dateOfCreation;
        this.requestInfo = requestInfo;
        this.actualTimeOfWork = actualTimeOfWork;
        this.crumbsUsers = crumbsUsers;
        this.crumbsProcessHistories = crumbsProcessHistories;
        this.crumbsAdditionalTools = crumbsAdditionalTools;
        this.crumbsRequestAndWorks = crumbsRequestAndWorks;
        this.crumbsDateAndTimes = crumbsDateAndTimes;
    }

    @Id
    @SequenceGenerator(name = "RequestSeq", sequenceName = "REQUEST_SEQUENCE", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "RequestSeq")
    @Column(name = "ID", unique = true, nullable = false, precision = 9, scale = 0)
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "CLIENT_ID", nullable = false)
    public CrumbsClient getCrumbsClient() {
        return this.crumbsClient;
    }

    public void setCrumbsClient(CrumbsClient crumbsClient) {
        this.crumbsClient = crumbsClient;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "STATE_ID", nullable = false)
    public CrumbsRequestsState getCrumbsRequestsState() {
        return this.crumbsRequestsState;
    }

    public void setCrumbsRequestsState(CrumbsRequestsState crumbsRequestsState) {
        this.crumbsRequestsState = crumbsRequestsState;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "DATE_OF_CREATION", nullable = false, length = 7)
    public Date getDateOfCreation() {
        return this.dateOfCreation;
    }

    public void setDateOfCreation(Date dateOfCreation) {
        this.dateOfCreation = dateOfCreation;
    }

    public void setDateOfCreationCustom(String dateOfCreation) {
        //SimpleDateFormat fmt = new SimpleDateFormat("dd/mm/yyyy  HH:mm");
        //SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //Date date = null;
        DateFormat formater = new SimpleDateFormat("dd/MM/yyyy HH:mm");

        try {
            java.util.Date parsedUtilDate = formater.parse(dateOfCreation);
            java.sql.Date sqltDate = new java.sql.Date(parsedUtilDate.getTime());
            this.dateOfCreation = parsedUtilDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    @Column(name = "REQUEST_INFO", length = 500)
    public String getRequestInfo() {
        return this.requestInfo;
    }

    public void setRequestInfo(String requestInfo) {
        this.requestInfo = requestInfo;
    }

    @Column(name = "ACTUAL_TIME_OF_WORK", precision = 9, scale = 0)
    public Integer getActualTimeOfWork() {
        return this.actualTimeOfWork;
    }

    public void setActualTimeOfWork(Integer actualTimeOfWork) {
        this.actualTimeOfWork = actualTimeOfWork;
    }

    @ManyToMany(cascade = {CascadeType.REFRESH, CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.EAGER)
    @JoinTable(name = "CRUMBS_REQUEST_ASSIGN", schema = "KMA1", joinColumns = {
            @JoinColumn(name = "REQUEST_ID", nullable = false, updatable = false)}, inverseJoinColumns = {
            @JoinColumn(name = "USER_ID", nullable = false, updatable = false)})
    public Set<CrumbsUser> getCrumbsUsers() {
        return this.crumbsUsers;
    }

    public void setCrumbsUsers(Set<CrumbsUser> crumbsUsers) {
        this.crumbsUsers = crumbsUsers;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "crumbsRequest")
    public Set<CrumbsProcessHistory> getCrumbsProcessHistories() {
        return this.crumbsProcessHistories;
    }

    public void setCrumbsProcessHistories(Set<CrumbsProcessHistory> crumbsProcessHistories) {
        this.crumbsProcessHistories = crumbsProcessHistories;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "crumbsRequest")
    public Set<CrumbsAdditionalTools> getCrumbsAdditionalTools() {
        return this.crumbsAdditionalTools;
    }

    public void setCrumbsAdditionalTools(Set<CrumbsAdditionalTools> crumbsAdditionalTools) {
        this.crumbsAdditionalTools = crumbsAdditionalTools;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "crumbsRequest")
    public Set<CrumbsRequestAndWork> getCrumbsRequestAndWorks() {
        return this.crumbsRequestAndWorks;
    }

    public void setCrumbsRequestAndWorks(Set<CrumbsRequestAndWork> crumbsRequestAndWorks) {
        this.crumbsRequestAndWorks = crumbsRequestAndWorks;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "crumbsRequest")
    public Set<CrumbsDateAndTime> getCrumbsDateAndTimes() {
        return this.crumbsDateAndTimes;
    }

    public void setCrumbsDateAndTimes(Set<CrumbsDateAndTime> crumbsDateAndTimes) {
        this.crumbsDateAndTimes = crumbsDateAndTimes;
    }

}


